package com.lutzyt.delivery.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lutzyt.delivery.bean.Address;
import com.lutzyt.delivery.mapper.AddressDAO;
import com.lutzyt.delivery.service.AddressService;
import com.lutzyt.delivery.service.AreaService;
import com.lutzyt.delivery.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author zyt
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class AddressServiceImpl extends ServiceImpl<AddressDAO, Address> implements AddressService {

  private AddressDAO addressDAO;
  private AreaService areaService;

  @Transactional(readOnly = true)
  @Override
  public Address findById(Integer id) {
    final Address address = addressDAO.selectById(id);
    this.setAddress(address);
    return address;
  }

  @Transactional(readOnly = true)
  @Override
  public IPage<Address> findByPage(int pageNum, int pageSize) {
    final Page<Address> page = addressDAO.selectPage(new Page<>(pageNum, pageSize), null);
    page.getRecords().forEach(this::setAddress);
    return page;
  }

  private void setAddress(Address address){
    address.setCity(areaService.findById(address.getCityId()).getAreaname());
    address.setProvince(areaService.findById(address.getProvinceId()).getAreaname());
    address.setCounty(areaService.findById(address.getCountyId()).getAreaname());
    address.setAddress(address.getProvince()+"/"+address.getCity()+"/"+address.getCounty()+"/"+address.getDetail());
  }

  @Override
  public void insert(Address address) {
    addressDAO.insert(address);
  }

  @Override
  public void update(Address address) {
    addressDAO.updateById(address);
  }

  @Override
  public void deleteById(Integer id) {
    addressDAO.deleteById(id);
  }

  @Override
  public void deleteByIds(List<String> ids) {
    addressDAO.deleteBatchIds(ids);
  }

  @Override
  public List<Address> findByCustomerId(Integer id) {
    final List<Address> list = this.lambdaQuery().eq(Address::getCustomerId, id).list();
    list.forEach(this::setAddress);
    return list;
  }

  @Override
  public void create(Address bean) {
    this.setAreaId(bean);
    addressDAO.insert(bean);
  }

  @Override
  public void customerUpdate(Address bean) {
    this.setAreaId(bean);
    addressDAO.updateById(bean);
  }

  private void setAreaId(Address bean){
    Integer provinceId = bean.getProvinceId();
    Integer cityId = bean.getCityId();
    Integer countyId = bean.getCountyId();
    bean.setCityId(areaService.findByCode(cityId).getId());
    bean.setCountyId(areaService.findByCode(countyId).getId());
    bean.setProvinceId(areaService.findByCode(provinceId).getId());
  }

  @Autowired
  public void setAddressDAO(AddressDAO addressDAO) {
    this.addressDAO = addressDAO;
  }

  @Autowired
  public void setAreaService(AreaService areaService) {
    this.areaService = areaService;
  }
}